package com.chance.cc.crawler.development.command.job.domain.weixin.api.simple;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.domain.weixin.WeixinCommonScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPageItem;

/**
 * @author lt
 * @version 1.0
 * @date 2021-06-28 11:16:14
 * @email okprog@sina.com
 */
public class WXApiSimpleKeywordsCrawlerSchedulerJob extends WeixinCommonScript {
    private static final String crawler_level = "simple";
    public static final String siteBiz = "article-trace";

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }
    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishCrawlerScheduleJobInfo(){

        CrawlerJob crawlerJobScheduler = crawlerJobScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJobScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("微信API补采采集任务");
        crawlerScheduleJob.setCrawlerKey(crawlerJobScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return crawlerJobScheduler;
    }

    public static CrawlerJob crawlerJobScheduler(){

        //文章采集
        CrawlerRequestRecord searchKWRecord = searchKWRecord();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",domain, kwSite, crawler_level, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domain, kwSite, crawler_level, "queue")))
                .kafkaResultPipeline(null, kafkaTopicForTraceJob, null)
                .fileResultPipeline(null, "/data/chance_crawler_test/data/weixin/weixin_medical_keyword_trace.log", false)
                .crawlerJobThreadNumber(3)
                .requestRecord(searchKWRecord)
                .supportRecord(getKwRecord(kwSite))
                .build();
        //配置采集输出
        crawlerJob.getScheduleTags().getCategoryTag().addKVTag(INTERACTION_DATA_DAY_FROM_START_TIME,1);
        crawlerJob.getScheduleTags().getCategoryTag().addKVTag(ARTICLE_DATA_DAY_FROM_NOW_TIME,3);
        crawlerJob.getScheduleTags().getCategoryTag().addKVTag(IS_OUTPUT_Interaction,true);
        return crawlerJob;
    }

    /**
     * 初始record
     * @return
     */
    public static CrawlerRequestRecord searchKWRecord(){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPageItem)
                .httpUrl("https://weixin.qq.com/")
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*6+14,null))//最大回溯时间范围
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,kwSite,"queue")))
                .needWashed(false)
                .needParsed(false)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        return requestRecord;
    }
}
